﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    /// <summary>
    /// 用户数据访问类
    /// </summary>
    public class UserInfoDAL
    {
        SqlCommand cmd = new SqlCommand();//定义Command对象

        /// 用户登录验证方法
        public bool UserLogin(string userid,string password)
        {
            cmd.CommandType = System.Data.CommandType.Text;//查询方式选用SQL语句
            cmd.CommandText = "SELECT *  FROM UserInfo WhERE userid='"+userid+"' and pwd='"+password+"'";

            try
            {
                object returnResult = DBHelper.GetScalar(cmd);//创建returnResult对象，存储SQL执行结果
                if (Convert.ToInt64(returnResult) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userid">用户号</param>
        /// <param name="userName">用户账号</param>
        /// <returns>用户信息数据集</returns>
        public DataSet GetUserInfo(string userid = null, string userName = null)
        {
            UserInfo obj = new UserInfo();
            if (userid == null)
                obj.UserID = "";
            else obj.UserID = userid;
            if (userName == null)
                obj.UserName = "";
            else obj.UserName = userName;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetUserInfo";//定义存储过程名字
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@userid", SqlDbType.VarChar, 11).Value = obj.UserID;
            cmd.Parameters.Add("@userName", SqlDbType.VarChar, 20).Value = obj.UserName;

            DataSet ds = DBHelper.GetDataSet(cmd);
            return ds;
        }
        public MemoryStream GetPhotoByUserID(string userid)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetPhotoByUserID";//定义存储过程名字
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@userid", SqlDbType.VarChar, 11).Value = userid;
            if (DBHelper.GetPhotoByUserID(cmd) != null)
                return DBHelper.GetPhotoByUserID(cmd);
            else
                return null;

        }
    }
}
